//
//  ATVJavaScriptController.m
//  FlashController
//
//  Created by Martin Schiøth Dyrby on 11/15/11.
//  Copyright 2011 Molamil. All rights reserved.
//

#import "ATVJavaScriptController.h"

@implementation ATVJavaScriptController
@synthesize browserView;
@synthesize swfName;
- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
    }
    
    [[NSNotificationCenter defaultCenter] 
        addObserver:self 
        selector: @selector(setSimpleValueHandler:) 
        name:@"setSimpleValue" 
        object: nil];
    [[NSNotificationCenter defaultCenter] 
        addObserver:self 
        selector: @selector(setBooleanValueHandler:) 
        name:@"setBooleanValue" 
        object: nil];
    
    [[NSNotificationCenter defaultCenter] 
        addObserver:self 
        selector: @selector(requestValueHandler:) 
        name:@"requestValue" 
        object: nil];
    
    [[NSNotificationCenter defaultCenter] 
     addObserver:self 
     selector: @selector(requestTypeDefHandler:) 
     name:@"requestTypeDef" 
     object: nil];
    
    [[NSNotificationCenter defaultCenter]
        addObserver:self
        selector:@selector(executeMethodHandler:) 
        name:@"executeMethod" 
        object:nil];
    
    [[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(executeBooleanMethodHandler:) 
     name:@"executeBooleanMethod" 
     object:nil];

    
    return self;
}

- (void) executeBooleanMethodHandler: (NSNotification *) note {
    NSDictionary *dict = (NSDictionary*) note.object;
    
    NSString* value = [dict valueForKey:@"value"];
    NSString* path = [dict valueForKey:@"path"];
    
	NSString* stepCallback;
	
	NSString* pathMinusProperty = [self findPathComponent: path];
	NSString* property = [self findPropertyComponent: path];
    
    stepCallback = @"document.getElementById('";
    stepCallback = [stepCallback stringByAppendingString: swfName];
    stepCallback = [stepCallback stringByAppendingString: @"').executeBooleanMethod('"];
    stepCallback = [stepCallback stringByAppendingString: pathMinusProperty];
    stepCallback = [stepCallback stringByAppendingString: @"','"];
    stepCallback = [stepCallback stringByAppendingString: property];	
    stepCallback = [stepCallback stringByAppendingString: @"','"];
    stepCallback = [stepCallback stringByAppendingString: value];
    stepCallback = [stepCallback stringByAppendingString: @"')"];
    
    NSLog(@"stepCallback: %@", stepCallback);
    
	[[browserView windowScriptObject] evaluateWebScript: stepCallback];
}

- (void) executeMethodHandler: (NSNotification *) note {
    NSDictionary *dict = (NSDictionary*) note.object;

    NSString* value = [dict valueForKey:@"value"];
    NSString* path = [dict valueForKey:@"path"];
    
	NSString* stepCallback;
	
	NSString* pathMinusProperty = [self findPathComponent: path];
	NSString* property = [self findPropertyComponent: path];

    if(value != nil) {
        
        stepCallback = @"document.getElementById('";
        stepCallback = [stepCallback stringByAppendingString: swfName];
        stepCallback = [stepCallback stringByAppendingString: @"').executeMethodWithParams('"];
        stepCallback = [stepCallback stringByAppendingString: pathMinusProperty];
        stepCallback = [stepCallback stringByAppendingString: @"','"];
        stepCallback = [stepCallback stringByAppendingString: property];	
        stepCallback = [stepCallback stringByAppendingString: @"','"];
        stepCallback = [stepCallback stringByAppendingString: value];
        stepCallback = [stepCallback stringByAppendingString: @"')"];
    } else {
        stepCallback = @"document.getElementById('";
        stepCallback = [stepCallback stringByAppendingString: swfName];
        stepCallback = [stepCallback stringByAppendingString: @"').executeMethod('"];
        stepCallback = [stepCallback stringByAppendingString: pathMinusProperty];
        stepCallback = [stepCallback stringByAppendingString: @"','"];
        stepCallback = [stepCallback stringByAppendingString: property];	
        stepCallback = [stepCallback stringByAppendingString: @"')"];
    }
    
	[[browserView windowScriptObject] evaluateWebScript: stepCallback];
}
-(void) setSimpleValueHandler: (NSNotification *) note {
    NSDictionary *dict = (NSDictionary*) note.object;
    
    NSString* value = [dict valueForKey:@"value"];
    NSString* path = [dict valueForKey:@"path"];
    
	NSString* stepCallback;
	
	NSString* pathMinusProperty = [self findPathComponent: path];
	NSString* property = [self findPropertyComponent: path];
	
	stepCallback = @"document.getElementById('";
	stepCallback = [stepCallback stringByAppendingString: swfName];
	stepCallback = [stepCallback stringByAppendingString: @"').setSimple('"];
	stepCallback = [stepCallback stringByAppendingString: pathMinusProperty];
	stepCallback = [stepCallback stringByAppendingString: @"','"];
	stepCallback = [stepCallback stringByAppendingString: property];	
	stepCallback = [stepCallback stringByAppendingString: @"','"];
	stepCallback = [stepCallback stringByAppendingString: value];
	stepCallback = [stepCallback stringByAppendingString: @"')"];
	
	[[browserView windowScriptObject] evaluateWebScript: stepCallback];
}


-(void) setBooleanValueHandler: (NSNotification *) note {
    NSDictionary *dict = (NSDictionary*) note.object;
    
    NSString* value = [dict valueForKey:@"value"];
    NSString* path = [dict valueForKey:@"path"];
    
	NSString* stepCallback;
	
	NSString* pathMinusProperty = [self findPathComponent: path];
	NSString* property = [self findPropertyComponent: path];
	
	stepCallback = @"document.getElementById('";
	stepCallback = [stepCallback stringByAppendingString: swfName];
	stepCallback = [stepCallback stringByAppendingString: @"').setBoolean('"];
	stepCallback = [stepCallback stringByAppendingString: pathMinusProperty];
	stepCallback = [stepCallback stringByAppendingString: @"','"];
	stepCallback = [stepCallback stringByAppendingString: property];	
	stepCallback = [stepCallback stringByAppendingString: @"','"];
	stepCallback = [stepCallback stringByAppendingString: value];
	stepCallback = [stepCallback stringByAppendingString: @"')"];
	
	[[browserView windowScriptObject] evaluateWebScript: stepCallback];
}


-(void) requestTypeDefHandler: (NSNotification *) note {
    NSDictionary *dict = (NSDictionary*) note.object;
    NSString* path = [dict valueForKey:@"path"];
    
	NSString* stepCallback;
	
	stepCallback = @"document.getElementById('";
	stepCallback = [stepCallback stringByAppendingString: swfName];
	stepCallback = [stepCallback stringByAppendingString: @"').requestTypeDef('"];
	stepCallback = [stepCallback stringByAppendingString: path];
	stepCallback = [stepCallback stringByAppendingString: @"')"];
	
	[[browserView windowScriptObject] evaluateWebScript: stepCallback];
}

- (void) requestValueHandler: (NSNotification *) note {
    NSDictionary *dict = (NSDictionary*) note.object;
    NSString* path = [dict valueForKey:@"path"];
    
	NSString* stepCallback;
	
	stepCallback = @"document.getElementById('";
	stepCallback = [stepCallback stringByAppendingString: swfName];
	stepCallback = [stepCallback stringByAppendingString: @"').requestValue('"];
	stepCallback = [stepCallback stringByAppendingString: path];
	stepCallback = [stepCallback stringByAppendingString: @"')"];
	
	[[browserView windowScriptObject] evaluateWebScript: stepCallback];
}



- (NSString*) findPathComponent: (NSString*) path {
	return [path substringToIndex:[path rangeOfString:@"." options: NSBackwardsSearch].location];
}
- (NSString*) findPropertyComponent: (NSString*) path {
	return [path substringFromIndex:([path rangeOfString:@"." options: NSBackwardsSearch].location+1)];
}
@end
